﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace FID_IceControl
{

    /// <summary>
    /// 压缩机工作状态
    /// </summary>
    public struct WorkingState
    {
        /// <summary>
        /// 上一周期采样时间
        /// </summary>
        public DateTime LastSCTime;
        /// <summary>
        /// 上一周期吸气端压力
        /// </summary>
        public float LastP1;
        /// <summary>
        /// 上一周期排气端压力
        /// </summary>
        public float LastP2;
        /// <summary>
        /// 采样时间
        /// </summary>
        public DateTime SCTime;
        /// <summary>
        /// 吸气端压力
        /// </summary>
        public float P1;
        /// <summary>
        /// 排气端压力
        /// </summary>
        public float P2;
        /// <summary>
        /// 压缩机工作频率,约定：
        /// </summary>
        public float Freq;
        /// <summary>
        /// 压缩机工作电流(2次侧)
        /// </summary>
        public float ECurrent;
        /// <summary>
        /// 吸气端温度
        /// </summary>
        public float T1;
        /// <summary>
        /// 排气端温度
        /// </summary>
        public float T2;
        /// <summary>
        /// 环境温度
        /// </summary>
        public float TEnvir;
        /// <summary>
        /// 吸气过热度
        /// </summary>
        public float Superheat;
        /// <summary>
        /// PID计算频率
        /// </summary>
        public float PIDOut;
        /// <summary>
        /// 写出频率
        /// </summary>
        public float WriteOut;
        /// <summary>
        /// 限制标志
        /// </summary>
        public int LimitFlag;
        /// <summary>
        /// 变频器输入电流(1次侧)
        /// </summary>
        public float ICurrent;

        /// <summary>
        /// 当前控制点（吸气压力）
        /// </summary>
        public float P1Set;
        /// <summary>
        /// 当前变频器读写方向
        /// </summary>
        public int MsgDirect;

        // 停机可以启动
        public static int STOP_CAN_START_FREQ = 0;
        // 停机不可以启动
        public static int STOP_CANNOT_START_FREQ = -1;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sTime"></param>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="freq"></param>
        /// <param name="eCurrent"></param>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <param name="tEnvir"></param>
        /// <param name="lastSCTime"></param>
        /// <param name="lastP1"></param>
        /// <param name="lastP2"></param>
        public WorkingState(DateTime sTime,
                            float p1,
                            float p2,
                            float freq,
                            float eCurrent,
                            float t1,
                            float t2,
                            float tEnvir,
                            DateTime lastSCTime,
                            float lastP1,
                            float lastP2,
                            float superheat,
                            float pidOut,
                            float writeOut,
                            int limitFlag,
                            float iCurrent,
                            float p1Set,
                            int megDirect
                            )
    {
            this.SCTime = sTime;
            this.P1 = p1;
            this.P2 = p2;
            this.Freq = freq;
            this.ECurrent = eCurrent;
            this.T1 = t1;
            this.T2 = t2;
            this.TEnvir = tEnvir;
            this.LastSCTime = lastSCTime;
            this.LastP1 = lastP1;
            this.LastP2 = lastP2;
            this.Superheat = superheat;
            this.PIDOut = pidOut;
            this.WriteOut = writeOut;
            this.LimitFlag = limitFlag;
            this.ICurrent = iCurrent;
            this.P1Set = p1Set;
            this.MsgDirect = megDirect;
    }

    /// <summary>
    /// 判断是否为停机状态
    /// </summary>
    /// <returns></returns>
    public bool IsWorking()
        {
            if (this.Freq == WorkingState.STOP_CANNOT_START_FREQ || this.Freq == WorkingState.STOP_CAN_START_FREQ) return false;
            else return true;
        }

        /// <summary>
        /// 判断是否为停机状态
        /// </summary>
        /// <returns></returns>
        public bool canStart()
        {
            if (this.Freq == WorkingState.STOP_CAN_START_FREQ) return true;
            else return false;
        }

        public string GTime()
        {
            return this.SCTime.ToString("O", new CultureInfo("en-us"));
        }

    }

    /// <summary>
    /// 压缩机工作函数代理
    /// </summary>
    /// <param name="paraArr"></param>
    /// <returns></returns>
    public delegate float WorkingFunction(List<float> paraArr);

    /// <summary>
    /// 压缩机工作函数的描述
    /// </summary>
    public static class CtlCommon
    {
        /// <summary>
        /// 本函数描述压缩机的静态函数
        /// </summary>
        /// <param name="paras">输入参数列表</param>
        /// <returns></returns>
        public static float WorkingFunction(List<float> paras)
        {
            return (float)Math.Sin(paras[0]);
        }
    }


}
